/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import entity.*;
import java.awt.*;
import java.awt.print.PageFormat;
import java.awt.print.PrinterException;
import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import javax.imageio.ImageIO;
import viewControl.KontaktPanel;

/**
 *
 * @author mender
 */
public class ElevInfoDocument extends Document {

    Elev elev;
    private Color lineColor;
    private Color headColor;
    private Color fillColor;
    private Color textColor;
    private Font boldFont;
    private Font plainFont;

    public ElevInfoDocument(Elev e) {
        elev = e;

        // Colors
        lineColor = new Color(127, 0, 0);
        headColor = new Color(255, 0, 0);
        fillColor = new Color(255, 223, 223);
        textColor = new Color(0, 0, 0);
        // Fonts
        plainFont = new Font(Font.SANS_SERIF, Font.PLAIN, 9);
        boldFont = new Font(Font.SANS_SERIF, Font.BOLD, 9);
    }

    @Override
    public int print(Graphics grphcs, PageFormat pf, int page) throws PrinterException {
        int printResult;

        Graphics2D g = (Graphics2D) grphcs;
        g.setFont(boldFont);
        FontMetrics fm = g.getFontMetrics();

        // Font Height
        int fontHeight = fm.getHeight();
        int portraitWidth = 100;
        int portraitHeight = 150;

        // Diverse mål på papiret.
        double xo = pf.getImageableX();
        double yo = pf.getImageableY();
        double width = pf.getImageableWidth();
        double height = pf.getImageableHeight();
        double center = width / 2.0;
        double middle = height / 2.0;
        g.translate(xo, yo);
        g.setColor(lineColor);

        // Mellemrum mellem linier.
        double spacing = fontHeight * 1.5;

        // Hvor langt nede er vi på papiret?
        int lineY;

        // Kontaktpersoner
        ArrayList<Kontaktperson> kps = new ArrayList();
        kps.addAll(elev.getKontaktpersons());
        int kpSection = (int) ((width - spacing - spacing) / 20.0);

        switch (page) {
            case 0:

                // Tegn logo.
                Point p = drawLogo(g, (int) spacing, (int) spacing, (int) width);
                lineY = (int) (spacing + p.y + spacing);

                g.setColor(fillColor);
                g.fillRect((int) spacing, lineY, (int) (spacing + spacing), (int) (spacing + spacing));
                g.setColor(textColor);
                g.drawString("Statusrapport", (int) (spacing + spacing + spacing + fontHeight), (int) (lineY + spacing + fontHeight / 2.5));
                lineY += spacing + spacing + spacing;

                // Portræt
                p = drawScaledImage(g, elev.getBilledeUri(), (int) spacing, lineY, portraitWidth, -1);
                if (p.y > 0) {
                    portraitHeight = p.y;
                }
                g.setColor(lineColor);
                g.drawRect((int) spacing - 2, lineY - 2, portraitWidth + 4, portraitHeight + 4);

                int portraitEnd = lineY + portraitHeight;

                // Elev Info
                int elevX = (int) (spacing + portraitWidth + spacing);
                int elevSection = (int) (((width - spacing) - elevX) / 4.0);
//                g.drawLine(elevX, lineY, (int) (width - spacing), lineY);
                g.drawString("Elev", elevX + fontHeight, (int) (lineY + fontHeight));

                // Navn og Cpr.Nr..
                lineY += spacing;
                g.setColor(fillColor);
                g.fillRect(elevX, lineY, (int) (width - spacing) - elevX, (int) spacing);
                g.setColor(lineColor);
                g.drawLine(elevX, lineY, (int) (width - spacing), lineY);
                g.setColor(headColor);
                g.setFont(plainFont);
                g.drawString("Navn:", elevX + fontHeight, (int) (lineY + fontHeight));
                g.drawString("Cpr. Nr.:", elevX + elevSection * 2, (int) (lineY + fontHeight));
                g.drawString("Indskrivnings dato:", elevX + elevSection * 3, (int) (lineY + fontHeight));
                lineY += spacing;
                g.setColor(textColor);
                g.drawString(elev.getFornavn() + " " + elev.getEfternavn(), elevX + fontHeight, (int) (lineY + fontHeight));
                g.drawString(elev.getCpr().substring(0, 6) + "-" + elev.getCpr().substring(6), elevX + elevSection * 2, (int) (lineY + fontHeight));
                // Formater dato'en. (DD.MM.ÅÅÅÅ)
                Calendar cal = Calendar.getInstance();
                cal.setTime(elev.getIndskrevet());
                String indskrevet = cal.get(Calendar.DATE) + "." + (cal.get(Calendar.MONTH) + 1) + "." + cal.get(Calendar.YEAR);
                g.drawString(indskrevet, elevX + elevSection * 3, (int) (lineY + fontHeight));

                // Adresse og Telefon nummer.
                lineY += spacing;
                g.setColor(fillColor);
                g.fillRect(elevX, lineY, (int) (width - spacing) - elevX, (int) spacing);
                g.setColor(headColor);
                g.setFont(plainFont);
                g.drawString("Adresse:", elevX + fontHeight, (int) (lineY + fontHeight));
                g.drawString("Mobil:", elevX + elevSection * 2, (int) (lineY + fontHeight));
                lineY += spacing;
                g.setColor(textColor);
                g.drawString(elev.getAdresse(), elevX + fontHeight, (int) (lineY + fontHeight));

                // Hent elevens telefon nummer...
                Object[] o = elev.getElevtlfs().toArray();
                if (o.length > 0) {
                    String tlf = ((Elevtlf) o[0]).getId().getTlfNr();
                    g.drawString(tlf, elevX + elevSection * 2, (int) (lineY + fontHeight));
                }

                // PostNr., By, Klassetrin og Indskrivningsdato.
                lineY += spacing;
                g.setColor(fillColor);
                g.fillRect(elevX, lineY, (int) (width - spacing) - elevX, (int) spacing);
                g.setColor(headColor);
                g.setFont(plainFont);
                g.drawString("PostNr.:", elevX + fontHeight, (int) (lineY + fontHeight));
                g.drawString("By:", elevX + elevSection, (int) (lineY + fontHeight));
                g.drawString("Klassetrin:", elevX + elevSection * 2, (int) (lineY + fontHeight));
                if (elev.getUdskrevet() != null) {
                    g.drawString("Udskrivnings dato:", elevX + elevSection * 3, (int) (lineY + fontHeight));
                }
                lineY += spacing;
                g.setColor(textColor);
                g.drawString(elev.getPostnrbynavn().getPostnr(), elevX + fontHeight, (int) (lineY + fontHeight));
                g.drawString(elev.getPostnrbynavn().getBynavn(), elevX + elevSection, (int) (lineY + fontHeight));
                g.drawString(elev.getNiveau().toString() + ". klasse", elevX + elevSection * 2, (int) (lineY + fontHeight));
                if (elev.getUdskrevet() != null) {
                    // Formater dato'en. (DD.MM.ÅÅÅÅ)
                    cal = Calendar.getInstance();
                    cal.setTime(elev.getUdskrevet());
                    String udskrevet = cal.get(Calendar.DATE) + "." + (cal.get(Calendar.MONTH) + 1) + "." + cal.get(Calendar.YEAR);
                    g.drawString(udskrevet, elevX + elevSection * 3, (int) (lineY + fontHeight));
                }

                // Afsluttende linie for Elev Info.
                lineY += spacing;
                g.setColor(lineColor);
                g.drawLine(elevX, lineY, (int) (width - spacing), lineY);

                // Hvis portrættet er højere end Elev Info, så skub Y-Positionen ned til under billedet.
                if (portraitEnd > lineY) {
                    lineY = portraitEnd;
                }

                // Kontaktpersoner
                lineY += spacing;
                g.drawLine((int) spacing, lineY, (int) (width - spacing), lineY);
                g.setFont(boldFont);
                g.drawString("Værge", (int) (spacing + fontHeight), lineY + fontHeight);
                lineY += spacing;
                g.drawLine((int) spacing, lineY, (int) (width - spacing), lineY);

                for (int i = 0; i < kps.size(); i++) {
                    Kontaktperson kp = kps.get(i);
                    if (kp.getRelation() == KontaktPanel.RELATION_FAR || kp.getRelation() == KontaktPanel.RELATION_MOR || kp.getRelation() == KontaktPanel.RELATION_VAERGE) {
                        lineY += spacing;
                        g.setColor(lineColor);
                        g.setFont(boldFont);
                        if (kp.getRelation() == KontaktPanel.RELATION_VAERGE) {
                            g.drawString("Andet", (int) (spacing + fontHeight), lineY + fontHeight);
                        } else {
                            g.drawString(Kontaktperson.getRelationString(kp.getRelation()), (int) (spacing + fontHeight), lineY + fontHeight);
                        }
                        lineY += spacing;
                        g.setColor(fillColor);
                        g.fillRect((int) spacing, lineY, (int) (width - spacing * 2), (int) spacing);
                        g.setColor(lineColor);
                        g.drawLine((int) spacing, lineY, (int) (width - spacing), lineY);
                        g.setColor(headColor);
                        g.setFont(plainFont);
                        g.drawString("Navn:", (int) (spacing + fontHeight), lineY + fontHeight);
                        g.drawString("Telefon:", (int) (spacing + fontHeight + kpSection * 10), lineY + fontHeight);
                        lineY += spacing;
                        g.setColor(textColor);
                        g.drawString(kp.getFornavn() + " " + kp.getEfternavn(), (int) (spacing + fontHeight), lineY + fontHeight);
                        Object[] kpo = kp.getKontakttlfs().toArray();
                        String kpPrivat = null;
                        String kpArbejde = null;
                        String kpMobil = null;
                        for (int j = 0; j < kpo.length; j++) {
                            Kontakttlf ktlf = (Kontakttlf) kpo[j];
                            switch (ktlf.getType()) {
                                case 1:
                                    kpPrivat = ktlf.getId().getTlfNr();
                                    break;
                                case 2:
                                    kpArbejde = ktlf.getId().getTlfNr();
                                    break;
                                case 3:
                                    kpMobil = ktlf.getId().getTlfNr();
                                    break;
                            }
                        }
                        g.drawString(kpPrivat, (int) (spacing + fontHeight + kpSection * 10), lineY + fontHeight);
                        lineY += spacing;
                        g.setColor(fillColor);
                        g.fillRect((int) spacing, lineY, (int) (width - spacing * 2), (int) spacing);
                        g.setColor(headColor);
                        g.drawString("Adresse:", (int) (spacing + fontHeight), lineY + fontHeight);
                        g.drawString("Mobil:", (int) (spacing + fontHeight + kpSection * 10), lineY + fontHeight);
                        lineY += spacing;
                        g.setColor(textColor);
                        g.drawString(kp.getAdresse(), (int) (spacing + fontHeight), lineY + fontHeight);
                        g.drawString(kpMobil, (int) (spacing + fontHeight + kpSection * 10), lineY + fontHeight);
                        lineY += spacing;
                        g.setColor(fillColor);
                        g.fillRect((int) spacing, lineY, (int) (width - spacing * 2), (int) spacing);
                        g.setColor(headColor);
                        g.drawString("PostNr.:", (int) (spacing + fontHeight), lineY + fontHeight);
                        g.drawString("By:", (int) (spacing + fontHeight + kpSection * 2), lineY + fontHeight);
                        g.drawString("E-mail:", (int) (spacing + fontHeight + kpSection * 10), lineY + fontHeight);
                        lineY += spacing;
                        g.setColor(textColor);
                        g.drawString(kp.getPostnrbynavn().getPostnr(), (int) (spacing + fontHeight), lineY + fontHeight);
                        g.drawString(kp.getPostnrbynavn().getBynavn(), (int) (spacing + fontHeight + kpSection * 2), lineY + fontHeight);
                        g.drawString(kp.getEmail(), (int) (spacing + fontHeight + kpSection * 10), lineY + fontHeight);
                        lineY += spacing;
                    }
                }

                printResult = PAGE_EXISTS;
                break;
            case 1:
                // Tegn logo.
                p = drawLogo(g, (int) spacing, (int) spacing, (int) width);
                lineY = (int) (spacing + p.y + spacing);

                g.setColor(fillColor);
                g.fillRect((int) spacing, lineY, (int) (spacing + spacing), (int) (spacing + spacing));
                g.setColor(textColor);
                g.drawString("Statusrapport", (int) (spacing + spacing + spacing + fontHeight), (int) (lineY + spacing + fontHeight / 2.5));
                lineY += spacing + spacing + spacing;

                // Kontaktpersoner
                g.setFont(boldFont);
                g.setColor(lineColor);
                g.drawString("Samarbejdspartnere", (int) (spacing + fontHeight), lineY + fontHeight);
                lineY += spacing;
                g.drawLine((int) spacing, lineY, (int) (width - spacing), lineY);

                g.setColor(fillColor);
                g.fillRect((int) (spacing), lineY, (int) (width - spacing - spacing), (int) spacing);
                g.setColor(lineColor);
                g.drawLine((int) (spacing), lineY, (int) (width - spacing), lineY);
                g.setColor(headColor);
                g.setFont(plainFont);
                g.drawString("Relation:", (int) (spacing + fontHeight), lineY + fontHeight);
                g.drawString("Navn:", (int) (spacing + fontHeight) + kpSection * 3, lineY + fontHeight);
                g.drawString("Kommune:", (int) (spacing + fontHeight) + kpSection * 7, lineY + fontHeight);
                g.drawString("Telefon:", (int) (spacing + fontHeight) + kpSection * 10, lineY + fontHeight);
                g.drawString("Mobil:", (int) (spacing + fontHeight) + kpSection * 12, lineY + fontHeight);
                g.drawString("E-mail:", (int) (spacing + fontHeight) + kpSection * 14, lineY + fontHeight);
                for (int i = 0; i < kps.size(); i++) {
                    Kontaktperson kp = kps.get(i);
                    if (kp.getRelation() != KontaktPanel.RELATION_FAR && kp.getRelation() != KontaktPanel.RELATION_MOR && kp.getRelation() != KontaktPanel.RELATION_VAERGE) {
                        lineY += spacing;
                        g.setColor(textColor);
                        g.setFont(plainFont);
                        g.drawString(Kontaktperson.getRelationString(kp.getRelation()), (int) (spacing + fontHeight), lineY + fontHeight);
                        g.drawString(kp.getFornavn() + " " + kp.getEfternavn(), (int) (spacing + fontHeight) + kpSection * 3, lineY + fontHeight);
                        g.drawString(kp.getRekvkommune(), (int) (spacing + fontHeight) + kpSection * 7, lineY + fontHeight);
                        Object[] kpo = kp.getKontakttlfs().toArray();
                        String kpPrivat = null;
                        String kpArbejde = null;
                        String kpMobil = null;
                        for (int j = 0; j < kpo.length; j++) {
                            Kontakttlf ktlf = (Kontakttlf) kpo[j];
                            switch (ktlf.getType()) {
                                case 1:
                                    kpPrivat = ktlf.getId().getTlfNr();
                                    break;
                                case 2:
                                    kpArbejde = ktlf.getId().getTlfNr();
                                    break;
                                case 3:
                                    kpMobil = ktlf.getId().getTlfNr();
                                    break;
                            }
                        }
                        if (kpArbejde != null) {
                            g.drawString(kpArbejde, (int) (spacing + fontHeight) + kpSection * 10, lineY + fontHeight);
                        }
                        if (kpMobil != null) {
                            g.drawString(kpMobil, (int) (spacing + fontHeight) + kpSection * 12, lineY + fontHeight);
                        }
                        g.drawString(kp.getEmail(), (int) (spacing + fontHeight) + kpSection * 14, lineY + fontHeight);
                    }
                }

                lineY += spacing * 2;

                g.setFont(boldFont);
                g.setColor(lineColor);
                g.drawString("Medicin", (int) spacing + fontHeight, lineY + fontHeight);
                lineY += spacing;

                g.drawLine((int) spacing, lineY, (int) (width - spacing), lineY);

                g.setFont(plainFont);
                g.setColor(textColor);
                ArrayList<Elevmedicin> emList = new ArrayList();
                emList.addAll(elev.getElevmedicins());
                for (int i = 0; i < emList.size(); i++) {
                    g.drawString(emList.get(i).getId().getMedicin(), (int) spacing + fontHeight, lineY + fontHeight);
                    lineY += spacing;
                }

                lineY += spacing;

                g.setFont(boldFont);
                g.setColor(lineColor);
                g.drawString("Allergier", (int) spacing + fontHeight, lineY + fontHeight);
                lineY += spacing;

                g.drawLine((int) spacing, lineY, (int) (width - spacing), lineY);

                g.setFont(plainFont);
                g.setColor(textColor);
                ArrayList<Elevallergi> eaList = new ArrayList();
                eaList.addAll(elev.getElevallergis());
                for (int i = 0; i < eaList.size(); i++) {
                    g.drawString(eaList.get(i).getId().getAllergi(), (int) spacing + fontHeight, lineY + fontHeight);
                    lineY += spacing;
                }
                printResult = PAGE_EXISTS;
                break;
            default:
                printResult = NO_SUCH_PAGE;
                break;
        }
        return printResult;
    }

    public Point drawLogo(Graphics2D g, int xPos, int yPos, int pageWidth) {
        g.setFont(boldFont);
        FontMetrics fm = g.getFontMetrics();
        int fontHeight = fm.getHeight();
        double spacing = fontHeight * 1.5;

        String adresse = "Glasmagervej 4, Fensmark";
        String pNum = "4684 Holmegaard";
        String tNum = "Telefon: 55 56 31 12";

        Calendar cal = Calendar.getInstance();
        String dato = "Fensmark, " + cal.get(Calendar.DATE) + ". " + monthName(cal.get(Calendar.MONTH)) + " " + cal.get(Calendar.YEAR);

        int aSize = fm.stringWidth(adresse);
        int pSize = fm.stringWidth(pNum);
        int tSize = fm.stringWidth(tNum);
        int dSize = fm.stringWidth(dato);

        Point p = drawScaledImage(g, "SkoleLogo.png", xPos, yPos, -1, 100);

        g.setColor(textColor);
        g.setFont(plainFont);
        g.drawString(adresse, (int) (pageWidth - spacing - aSize), yPos + fontHeight);
        g.drawString(pNum, (int) (pageWidth - spacing - pSize), (int) (yPos + spacing + fontHeight));
        g.drawString(tNum, (int) (pageWidth - spacing - tSize), (int) (yPos + spacing * 2 + fontHeight));
        g.setFont(boldFont);
        g.drawString(dato, (int) (pageWidth - spacing - dSize), (int) (yPos + spacing * 4 + fontHeight));
        return p;
    }

    public String monthName(int month) {
        String m = "";

        switch (month) {
            case 0:
                m = "Januar";
                break;
            case 1:
                m = "Februar";
                break;
            case 2:
                m = "Marts";
                break;
            case 3:
                m = "April";
                break;
            case 4:
                m = "Maj";
                break;
            case 5:
                m = "Juni";
                break;
            case 6:
                m = "Juli";
                break;
            case 7:
                m = "August";
                break;
            case 8:
                m = "September";
                break;
            case 9:
                m = "Oktober";
                break;
            case 10:
                m = "November";
                break;
            case 11:
                m = "December";
                break;
        }
        return m;
    }

    public void drawImage(Graphics2D g, String fileName, int x, int y) {
        Image img;
        try {
            img = ImageIO.read(new File(fileName));
            g.drawImage(img, x, y, null);
        } catch (Exception ex) {
            // Hvis billedfilen ikke findes eller vi får en null pointer,
            // så lader vi bare være med at tegne billedet.
        }
    }

    public Point drawScaledImage(Graphics2D g, String fileName, int x, int y, int width, int height) {
        Image img;
        Point p;
        try {
            img = ImageIO.read(new File(fileName));
            img = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
            p = new Point(img.getWidth(null), img.getHeight(null));
            g.drawImage(img, x, y, null);
        } catch (Exception ex) {
            // Hvis billedfilen ikke findes eller vi får en null pointer,
            // så lader vi bare være med at tegne billedet.
            p = new Point(0, 0);
        }
        return p;
    }

    @Override
    public String getJobName() {
        return "ELEV: " + elev.getFornavn() + " " + elev.getEfternavn();
    }
}
